package mia.beans;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.apache.myfaces.custom.fileupload.UploadedFile;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import mia.dao.AdvertisementEntityDAO;
import mia.entity.AdvertisementEntity;
import mia.entity.AdvertisementTypeEntity;
import mia.entity.AttachmentEntity;
import mia.entity.CategoryEntity;
import mia.entity.UserEntity;
import mia.util.HibernateManager;

@ManagedBean(name = "displayAdvertisementsBean")
@RequestScoped
public class DisplayAdvertisementsBean {

	@ManagedProperty("#{userLoginBean.user}")
	private UserEntity user;

	@ManagedProperty("#{advertisementTypeBean.types}")
	private Map<String, AdvertisementTypeEntity> types;

	@ManagedProperty("#{categoryBean.categories}")
	private Map<String, CategoryEntity> categories;

	private String titleLike;
	private String contentLike;
	private Date addedAfter;
	private Date activeToDate;
	private String category;
	private String type;

	private AdvertisementEntity selectedAdvertisement = new AdvertisementEntity();
	private AdvertisementEntity selectedAdvertisementEdit = new AdvertisementEntity();
	private AdvertisementEntity selectedAdvertisementDelete = new AdvertisementEntity();

	@ManagedProperty(value = "10")
	private int size;

	public List<AdvertisementEntity> get10AdvertisementList() {
		return AdvertisementEntityDAO.get10LastAdvertisements(size);
	}

	public List<AdvertisementEntity> getUserAdvertisementList() {
		return AdvertisementEntityDAO.getUserAdvertisements(getUser());
	}

	public List<AdvertisementEntity> getAdvertisementsFromSearch() {
		return AdvertisementEntityDAO.getAdvertisements(titleLike, contentLike,
				addedAfter, activeToDate, categories.get(category),
				types.get(type));
	}

	public void tryEdit() {
		Session session = HibernateManager.getSession();
		session.beginTransaction();

		FacesContext context = FacesContext.getCurrentInstance();

		AdvertisementEntity editADV = AdvertisementEntityDAO
				.getAdvertisementById(session,
						selectedAdvertisementEdit.getAdvertisement_id());

		if (editADV != null) {

			if (selectedAdvertisementEdit.getTitle() != null
					&& !selectedAdvertisementEdit.getTitle().equals(
							editADV.getTitle()))
				editADV.setTitle(selectedAdvertisementEdit.getTitle());

			if (selectedAdvertisementEdit.getContent() != null
					&& !selectedAdvertisementEdit.getContent().equals(
							editADV.getContent()))
				editADV.setContent(selectedAdvertisementEdit.getContent());

			if (selectedAdvertisementEdit.getStartDate() != null
					&& !selectedAdvertisementEdit.getStartDate().equals(
							editADV.getStartDate()))
				editADV.setStartDate(selectedAdvertisementEdit.getStartDate());

			if (selectedAdvertisementEdit.getExpireDate() != null
					&& !selectedAdvertisementEdit.getExpireDate().equals(
							editADV.getExpireDate()))
				editADV.setExpireDate(selectedAdvertisementEdit.getExpireDate());

			if (selectedAdvertisementEdit.getType() != null
					&& !selectedAdvertisementEdit.getType().equals(
							editADV.getType()))
				editADV.setType(selectedAdvertisementEdit.getType());

			if (selectedAdvertisementEdit.getCategory() != null
					&& !selectedAdvertisementEdit.getCategory().equals(
							editADV.getCategory()))
				editADV.setCategory(selectedAdvertisementEdit.getCategory());
			session.save(editADV);
			session.getTransaction().commit();
			context.addMessage("change", new FacesMessage(
					FacesMessage.SEVERITY_INFO, "Edytowano ogłoszenie!", ""));
		}
		session.close();
	}

	@SuppressWarnings("unused")
	public void tryDelete() {
		Session session = HibernateManager.getSession();
		session.beginTransaction();

		FacesContext context = FacesContext.getCurrentInstance();

		AdvertisementEntity adv = AdvertisementEntityDAO.getAdvertisementById(
				session, selectedAdvertisementDelete.getAdvertisement_id());
		if (adv != null) {
			Criteria crit = session.createCriteria(AttachmentEntity.class);
			crit.add(Restrictions.eq("advertisement", adv));
			@SuppressWarnings("unchecked")
			List<AttachmentEntity> lista = crit.list();

			for (int i = 0; i < lista.size(); i++)
				for (AttachmentEntity att : lista) {
					session.delete(lista.get(i));
				}
			session.delete(adv);

			session.getTransaction().commit();
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_INFO, "Usunięto ogłoszenie", ""));

		}
		session.close();
	}
	
	private List<String> attaList()
	{/*
		Session session = HibernateManager.getSession();
		Criteria crit = session.createCriteria(AttachmentEntity.class);
		crit.add(Restrictions.eq("advertisement", selectedAdvertisement));
		List<AttachmentEntity> att2 = crit.list();
		List<String> atta = new ArrayList<String>();
		for(AttachmentEntity att: att2)
		{
			if(!att.getPath().isEmpty())
				atta.add(att.getPath());
		}	Collection<AttachmentEntity> att2 = selectedAdvertisement.getAttachments();
		List<String> atta = new ArrayList<String>();
		for(AttachmentEntity att: att2)
		{
			
				atta.add(att.getPath());
		}
		/*/		return null;
	}

	// GETTERS AND SETTERS

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public UserEntity getUser() {
		return user;
	}

	public void setUser(UserEntity user) {
		this.user = user;
	}

	public Map<String, AdvertisementTypeEntity> getTypes() {
		return types;
	}

	public void setTypes(Map<String, AdvertisementTypeEntity> types) {
		this.types = types;
	}

	public Map<String, CategoryEntity> getCategories() {
		return categories;
	}

	public void setCategories(Map<String, CategoryEntity> categories) {
		this.categories = categories;
	}

	public AdvertisementEntity getSelectedAdvertisement() {
		return selectedAdvertisement;
	}

	public void setSelectedAdvertisement(
			AdvertisementEntity selectedAdvertisement) {
		this.selectedAdvertisement = selectedAdvertisement;
	}

	public String getTitleLike() {
		return titleLike;
	}

	public void setTitleLike(String titleLike) {
		this.titleLike = titleLike;
	}

	public String getContentLike() {
		return contentLike;
	}

	public void setContentLike(String contentLike) {
		this.contentLike = contentLike;
	}

	public Date getAddedAfter() {
		return addedAfter;
	}

	public void setAddedAfter(Date addedAfter) {
		this.addedAfter = addedAfter;
	}

	public Date getActiveToDate() {
		return activeToDate;
	}

	public void setActiveToDate(Date activeToDate) {
		this.activeToDate = activeToDate;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public AdvertisementEntity getSelectedAdvertisementDelete() {
		return selectedAdvertisementDelete;
	}

	public void setSelectedAdvertisementDelete(
			AdvertisementEntity selectedAdvertisementDelete) {
		this.selectedAdvertisementDelete = selectedAdvertisementDelete;
	}

	public AdvertisementEntity getSelectedAdvertisementEdit() {
		return selectedAdvertisementEdit;
	}

	public void setSelectedAdvertisementEdit(
			AdvertisementEntity selectedAdvertisementEdit) {
		this.selectedAdvertisementEdit = selectedAdvertisementEdit;
	}


	private List<AttachmentEntity> images;

	public List<AttachmentEntity> getImages() {
		return images;
	}

	public void setImages(List<AttachmentEntity> images) {
		this.images = images;
	}
	
	public String goToResult()
	{
		//images = attaList();
		
		return "result";
	}
	

	

}
